package com.mnt.washio.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mnt.washio.R;
import com.mnt.washio.objects.Address;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.mnt.washio.utils.network.ws.AddressWSControl;
import com.mnt.washio.utils.network.ws.BaseWSControl;
import com.mnt.washio.utils.network.ws.BaseWSControl.WebServiceFlag;
import com.mnt.washio.views.CongraDialog;
import com.mnt.washio.views.CustomDialog;
import com.parse.ParseUser;

public class RegisterStep2Activity extends BaseActivityNoTitle implements
		OnClickListener, WebServiceCommunicatorListener {
	private ImageView myImgArrowBottom;
	private Button myBtnAddAddress;
	private CustomDialog myDialog;
	private int myPosition;
	private TextView myTvTitle;

	private EditText mEdtFirstAdd, mEdtSecondAdd, mEdtZipCode, mEdtNote;
	private TextView mTvName;
	private List<Object> arrName;
	private ProgressDialog pd;
	private Address mCurAdd;
	private boolean isRegisterStep = false;

	@Override
	public void initViews() {
		// TODO Auto-generated method stub
		overridePendingTransition(R.anim.slide_in_from_right, R.anim.anim_alpha);
		myImgArrowBottom = (ImageView) findViewById(R.id.img_arrow_bottom);
		myBtnAddAddress = (Button) findViewById(R.id.btn_add_address);
		myTvTitle = (TextView) findViewById(R.id.tv_title);

		mEdtFirstAdd = (EditText) findViewById(R.id.edt_adr_lin_1);
		mEdtSecondAdd = (EditText) findViewById(R.id.edt_adr_lin_2);
		mEdtZipCode = (EditText) findViewById(R.id.edt_zip_code);
		mEdtNote = (EditText) findViewById(R.id.edt_note);
		mTvName = (TextView) findViewById(R.id.tv_name);

		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		pd.setMessage("Saving...");
	}

	@Override
	public void addListener() {
		// TODO Auto-generated method stub
		myImgArrowBottom.setOnClickListener(this);
		myBtnAddAddress.setOnClickListener(this);
		mTvName.setOnClickListener(this);
	}

	@Override
	public void initProperties() {
		// TODO Auto-generated method stub
		isRegisterStep = getIntent().getBooleanExtra("REGISTER", false);
		if (!isRegisterStep) {
			myTvTitle.setVisibility(View.INVISIBLE);
		}

		arrName = new ArrayList<Object>();
		final String[] array = getResources().getStringArray(
				R.array.arr_addition);
		for (int i = 0; i < array.length; i++) {
			arrName.add(array[i]);
		}

		myPosition = 0;
		mTvName.setText(array[myPosition]);

		fillData();
	}

	private void fillData() {
		mCurAdd = (Address) getIntent().getSerializableExtra("ADDRESS");
		if (mCurAdd != null) {

			mEdtFirstAdd.setText(mCurAdd.getAddress());
			mEdtSecondAdd.setText(mCurAdd.getApartmentNumber());
			mEdtZipCode.setText(mCurAdd.getZipCode());
			mEdtNote.setText(mCurAdd.getNote());
			mTvName.setText(mCurAdd.getName());

			myBtnAddAddress.setText(R.string.txt_update_adr);
		}
	}

	@Override
	public int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_register_step_2;
	}

	public void showDialogAddition() {

		myDialog = new CustomDialog(this, arrName, getResources().getString(
				R.string.address_type), myPosition);
		myDialog.setOnClickItem(new CustomDialog.OnClickItem() {

			@Override
			public void Click(int position) {
				// TODO Auto-generated method stub
				position -= 1;
				mTvName.setText(arrName.get(position).toString());
				myPosition = position;
			}
		});
		myDialog.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_name:
			showDialogAddition();
			break;
		case R.id.img_arrow_bottom:

			break;
		case R.id.btn_add_address:
			if (mCurAdd != null) {
				validateAndSave(false); // Update
			} else {
				validateAndSave(true); // Create
			}
			break;
		default:
			break;
		}
	}

	private void validateAndSave(boolean flag) {
		String objectId = (mCurAdd != null) ? mCurAdd.getObjectId() : "";
		String firstAdd = mEdtFirstAdd.getText().toString().trim();
		String secondAdd = mEdtSecondAdd.getText().toString().trim();
		String zipCode = mEdtZipCode.getText().toString().trim();
		String name = mTvName.getText().toString().trim();
		String note = mEdtNote.getText().toString().trim();

		if (firstAdd.length() == 0 && secondAdd.length() == 0) {
			Statics.showToast(this, "Please enter your address");
			return;
		}

		if (zipCode.length() == 0) {
			Statics.showToast(this, "Please enter your zip code");
			return;
		}

		String userObjectId = "";
		userObjectId = ParseUser.getCurrentUser().getObjectId();
		new AddressWSControl(this, this, userObjectId, objectId, firstAdd,
				secondAdd, zipCode, name, note, flag).execute();
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		if (flag == WebServiceFlag.ADD_ADDRESS) {
			pd.show();
		}
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		if (flag == WebServiceFlag.ADD_ADDRESS) {
			pd.dismiss();
			Statics.showToast(this, error + "");
		}
	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		if (flag == WebServiceFlag.ADD_ADDRESS) {
			pd.dismiss();

			if (result.equals("Successful")) {
				if (getIntent().getBooleanExtra("REGISTER", false)) {
					Intent i = new Intent(this, RegisterStep3Activity.class);
					i.putExtra("REGISTER",
							getIntent().getBooleanExtra("REGISTER", false));
					startActivity(i);
					finish();
				} else {
					Statics.showToast(this, result.toString());
					setResult(RESULT_OK);
					finish();
				}
			} else {
				Statics.showToast(this, result.toString());
			}
		}
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
	}

	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		super.onBackPressed();
	}
}
